package com.china08.yxyapi.api.manage.edu.platform.resource.yxylectureprepack;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLecturePrePackSubjectController {

    @Autowired
    YxyResBookRepository yxyResBookRepository;

    @ApiOperation(value = "通过学段获取学科")
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/subject/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String phaseId = reqModel.phaseId;
        List<Object> list = yxyResBookRepository.findDistinctSubjectNameByPhaseId(phaseId);

        List<RespModel> returnList = new ArrayList<RespModel>();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Object object = list.get(i);
                if (null != object) {
                    Object[] objects = (Object[]) list.get(i);
                    RespModel respModel = new RespModel();
                    respModel.subjectId = objects[1] + "";
                    respModel.subjectName = objects[0] + "";
                    returnList.add(respModel);
                }
            }
        }
        return returnList;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phaseId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(phaseId), "学段为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String subjectId;
        public String subjectName;
    }
}
